Skip to content

Method: checkPossibleOutcome(Map, SspAnswer, SspAnswer)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel23-Ssp.
5: *
6: * Ipspiel23-Ssp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8: * version.
9: *
10: * Ipspiel23-Ssp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License along with ipspiel23-Ssp. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel23.ssp.domain.impl;
17:
18: import de.fhdw.gaming.core.domain.GameException;
19: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayer;
20: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayerBuilder;
21: import de.fhdw.gaming.ipspiel23.ssp.domain.impl.outcomes.SspAnswer;
22:
23: import java.util.Collections;
24: import java.util.Map;
25: import java.util.Optional;
26:
27: /**
28: * Implements {@link SspPlayerBuilder}.
29: */
30: final class SspPlayerBuilderImpl implements SspPlayerBuilder {
31:
32: /**
33: * The name of the player.
34: */
35: private Optional<String> name;
36: /**
37: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
38: * for the second-level map is the answer of the second player.
39: */
40: private Optional<Map<SspAnswer, Map<SspAnswer, Double>>> possibleOutcomes;
41:
42: /**
43: * Creates an {@link SspPlayerBuilderImpl}.
44: */
45: SspPlayerBuilderImpl() {
46: this.name = Optional.empty();
47: this.possibleOutcomes = Optional.empty();
48: }
49:
50: @Override
51: public SspPlayerBuilderImpl changeName(final String newName) {
52: this.name = Optional.of(newName);
53: return this;
54: }
55:
56: @Override
57: public SspPlayerBuilder changePossibleOutcomes(
58: final Map<SspAnswer, Map<SspAnswer, Double>> newPossibleOutcomes) {
59: this.possibleOutcomes = Optional.of(newPossibleOutcomes);
60: return this;
61: }
62:
63: @Override
64: public SspPlayer build() throws GameException {
65: return new SspPlayerImpl(
66: this.name.orElseThrow(),
67: this.checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
68: }
69:
70: /**
71: * Checks if all possible outcomes are defined for a player.
72: *
73: * @param outcomes The possible outcomes for the player.
74: */
75: private Map<SspAnswer, Map<SspAnswer, Double>> checkPossibleOutcomes(
76: final Map<SspAnswer, Map<SspAnswer, Double>> outcomes) {
77:
78: this.checkPossibleOutcome(outcomes, SspAnswer.PAPER, SspAnswer.PAPER);
79: this.checkPossibleOutcome(outcomes, SspAnswer.PAPER, SspAnswer.STONE);
80: this.checkPossibleOutcome(outcomes, SspAnswer.PAPER, SspAnswer.SCISSORS);
81:
82: this.checkPossibleOutcome(outcomes, SspAnswer.STONE, SspAnswer.STONE);
83: this.checkPossibleOutcome(outcomes, SspAnswer.STONE, SspAnswer.PAPER);
84: this.checkPossibleOutcome(outcomes, SspAnswer.STONE, SspAnswer.SCISSORS);
85:
86: this.checkPossibleOutcome(outcomes, SspAnswer.SCISSORS, SspAnswer.STONE);
87: this.checkPossibleOutcome(outcomes, SspAnswer.SCISSORS, SspAnswer.PAPER);
88: this.checkPossibleOutcome(outcomes, SspAnswer.SCISSORS, SspAnswer.SCISSORS);
89:
90: return outcomes;
91: }
92:
93: /**
94: * Checks if a given outcome is defined for a player.
95: *
96: * @param outcomes The possible outcomes for the player.
97: * @param firstChoice The choice of the first player.
98: * @param secondChoice The choice of the second player.
99: */
100: private void checkPossibleOutcome(final Map<SspAnswer, Map<SspAnswer, Double>> outcomes,
101: final SspAnswer firstChoice,
102: final SspAnswer secondChoice) {
103:• if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
104: throw new IllegalArgumentException(
105: String.format(
106: "No outcome defined for player '%s' and combination %s/%s.",
107: this.name,
108: firstChoice.toString(),
109: secondChoice.toString()));
110: }
111: }
112: }